package com.ostrich.manpower.common;

import java.io.IOException;
import java.io.InputStream;
import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFDateUtil;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import com.base.util.DateUtil;
import com.ostrich.manpower.bean.TostHrMealaddinfo;
public class ImportUtil {


	 public static HSSFSheet getSheet(InputStream fis) {  
	        //这里是解析出来的Excel的数据存放的List集合  
	        List<TostHrMealaddinfo> infos = new ArrayList<TostHrMealaddinfo>();  
	                //这里是解析出来的Excel中的每一条数据封装的实体BEAN.  
	        TostHrMealaddinfo employeeInfo = null;  
	          
	        try {  
	            //创建Excel工作薄  
	            HSSFWorkbook hwb = new HSSFWorkbook(fis);  
	            //得到第一个工作表  
	            HSSFSheet sheet = hwb.getSheetAt(0);  
	          
	            //日期格式化  
	            DateFormat ft = new SimpleDateFormat("yyyy-MM-dd");  
	            //遍历该表格中所有的工作表，i表示工作表的数量 getNumberOfSheets表示工作表的总数   
	            for(int i = 0; i < hwb.getNumberOfSheets(); i++) {  
	                sheet = hwb.getSheetAt(i);  
	                return sheet; //return first-sheet
	            } 
	        } catch (IOException e) {  
	            e.printStackTrace();  
	        }  
	        return null;  
	    }
	 
	
	 
	    //判断从Excel文件中解析出来数据的格式  
	public static String getCellValue(HSSFCell cell) {
		String value = null;
		if (cell != null) {
			int tp=cell.getCellType();
			//特殊判断时间
			if(HSSFCell.CELL_TYPE_NUMERIC == tp && HSSFDateUtil.isCellDateFormatted(cell)){
				Date dt = cell.getDateCellValue();
				value = DateUtil.getDateLong(dt);
				return value;  
			}
			// 简单的查检列类型
			switch (cell.getCellType()) {
			case HSSFCell.CELL_TYPE_STRING:// 字符串
				value = cell.getRichStringCellValue().getString();
				break;
			case HSSFCell.CELL_TYPE_NUMERIC:// 数字
				BigDecimal bd = new BigDecimal(cell.getNumericCellValue());
				Float dd = bd.setScale(2,4).floatValue();
				value = dd + "";
				break;
			case HSSFCell.CELL_TYPE_BLANK:
				value = "";
				break;
			case HSSFCell.CELL_TYPE_FORMULA:
				//value = String.valueOf(cell.getCellFormula());
				value = String.valueOf(cell.getNumericCellValue());
				break;
			case HSSFCell.CELL_TYPE_BOOLEAN:// boolean型值
				value = String.valueOf(cell.getBooleanCellValue());
				break;
			case HSSFCell.CELL_TYPE_ERROR:
				value = String.valueOf(cell.getErrorCellValue());
				break;
			default:
				break;
			}
		}
	        return value;  
	    }  
	    
	    
	    
	   
}
